/*
 * @(#) Logger.java 1.0 2004-4-20
 * @author xingxiaojie
 *
 * Copyright 2003 by LongTop Corporation.
 * 10 Hubinbei Road, XiaMen, PRC 361012
 * All rights reserved.
 *
 * This software is the confidential and proprietary INFOrmation of
 * LongTop Corporation ("Confidential INFOrmation"). You
 * shall not disclose such Confidential INFOrmation and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */
package net.comm.util;

import java.io.*;
import java.util.Properties;

import net.comm.common.GlbFinal;

/**
 * @author xingxiaojie
 * @version 1.0
 * @creatime 2004-4-20
 */
public class Logger {
	private String configfile=GlbFinal.ConfigName;
	private int level;			//当前日记类日记信息级别
	private String filePrefix;		//文件名前缀
	private PrintWriter out;		//日记器的输出流
	private String title="";		//日记器日记信息提示格式
	private String diveflag;		//日记是否划分文件标志
	private String outtype;		//日记输出类型:File,Standard	
	private String filepostfix;	//日记文件后缀
	private static Logger me=new Logger();
	
	private Logger() {
		try{
			readConfig(configfile);
		}
		catch(Exception e) {
			System.out.println("读取日志配置文件失败，使用缺省配置");
			defaultConfig();
		}
		initOut();
	}

	/*
	 * 取得初始化输出流
	 */
	private void initOut(){
		try {
			if(outtype.equals("Standard")) {
				out=new PrintWriter(System.out);
				System.out.println("日志使用标准输出流");
			}
			else {
				if(diveflag.equals("true")) {
					out=new PrintWriter(new FileOutputStream(filePrefix+"."+SWFUtil.getCurrentDate("yyyyMMdd"),true));
					System.out.println("日志输出到文件"+filePrefix+".*");
				}
				else {
					out=new PrintWriter(new FileOutputStream(filePrefix,true));
					System.out.println("日志输出到文件"+filePrefix);
				}			
			}			
		}
		catch(Exception e) {
			out=new PrintWriter(System.out);
			System.out.println("日志输出到文件失败，使用标准输出流");
		}
	}

		
	/*
	 * 读取SWIFT项目配置文件中Logger的配置
	 */
	private void readConfig(String filename) throws Exception {
		Properties prop=new Properties();
		prop.load(new FileInputStream(filename));
		title=prop.getProperty("Logger.title");
		outtype=prop.getProperty("Logger.outtype");
		diveflag=prop.getProperty("Logger.diveflag");	
		filePrefix=prop.getProperty("Logger.filename");
		level=Integer.parseInt(prop.getProperty("Logger.level"));
		filepostfix=SWFUtil.getCurrentDate("yyyyMMdd");
	}
	
	/*
	 * 读取配置文件失败时的缺省配置
	 */
	private void defaultConfig() {
		String str;
		str="["+SWFUtil.getCurrentDate("yy-MM-dd")+" ";
		str+=SWFUtil.getCurrentDate("HH:mm")+"]";
		title=str;
		outtype=GlbFinal.Logger_OutType;
		diveflag=GlbFinal.Logger_DiveFlag;
		filePrefix=GlbFinal.Logger_FilePrefix;
		level=GlbFinal.Logger_Level;
		filepostfix=SWFUtil.getCurrentDate("yyyyMMdd");
	}
	
	/*
	 * 日记器运行期间,更改日记配置
	 */
	public static void setLogger(String aFile,int lev) {
		me.level=lev;
		me.filePrefix=aFile;
	}
	
	public static int getLevel() {
		return me.level;
	}

	public static void setLevel(int i) {
		me.level = i;
	}
	
	/*
	 * 取得当前输出流
	 */
	private void flashOut(){
		try {
			//判断文件后缀是否改变
			if(outtype.equals("Standard"))	return;
			if(!filepostfix.equals(SWFUtil.getCurrentDate("yyyyMMdd")) && diveflag.equals("true")) {
				me.out.flush();
				me.out.close();
				me.out=new PrintWriter(new FileOutputStream(filePrefix+"."+SWFUtil.getCurrentDate("yyyyMMdd"),true));
			}
		}
		catch(Exception e) {
			me.out=new PrintWriter(System.out);
			System.out.println("日志刷新流失败，使用标准输出流");			
		}
	}

	/*
	 * 记录日记信息
	 */
	private synchronized void record(int lev,String modul,String msg) {
		if(lev>=me.level) { 
			String tmp=new String(me.title);
			
			String str;
			str="["+SWFUtil.getCurrentDate("yy-MM-dd")+" ";
			str+=SWFUtil.getCurrentDate("HH:mm")+"]";
			
			String ss="Logger";
			switch(lev) {
				case LogLevel.debug: ss="DEBUG";break;
				case LogLevel.info: ss="INFO";break;
				case LogLevel.warn: ss="WARN";break;
				case LogLevel.error: ss="ERROR";break;
				case LogLevel.fatal: ss="FATAL";break;
			}
			if(SWFUtil.isStrEmpty(modul)) modul="System";
			tmp+="("+modul+")"+ss+":";
			flashOut();
			me.out.println(tmp+str+msg);
			me.out.flush();
		}
	}
	
	public static void debug(String modul,String msg) {
		me.record(LogLevel.debug,modul,msg);
	}
	public static void info(String modul,String msg) {
		me.record( LogLevel.info,modul,msg);
	}
	public static void warn(String modul,String msg) {
		me.record( LogLevel.warn,modul,msg);
	}
	public static void error(String modul,String msg) {
		me.record( LogLevel.error,modul,msg);
	}
	public static void fatal(String modul,String msg) {
		me.record( LogLevel.fatal,modul,msg);
	}

	public static void main(String[] args) {
		Logger.error("Test","SWIFT Error");
		Logger.fatal("Test","Swift Exit");	
		Logger.setLogger("Log.txt",LogLevel.debug);
		Logger.error("Test","SWIFT Error");
		Logger.fatal("Test","Swift Exit");	
	}
}
